package com.htgd.gardenexpo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dto.GeConCompetitionSportsDTO;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConCompetitionSports;
import com.htgd.gardenexpo.entity.GeConSocialTopics;
import com.htgd.gardenexpo.entity.GeMyFeedback;
import com.htgd.gardenexpo.page.CompetitionSportsQuery;
import com.htgd.gardenexpo.page.SocialTopicsQuery;
import com.htgd.gardenexpo.service.IGeConSocialTopicsService;
import com.htgd.gardenexpo.vo.GeConCompetitionSportsVO;
import com.htgd.gardenexpo.vo.GeConSocialTopicsVO;
import com.htgd.gardenexpo.vo.SocialTopicNumsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 社交话题 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-08
 */
@RestController
@RequestMapping("/api/socialTopics")
@Api(tags = "社交话题")
@Slf4j
public class GeConSocialTopicsController {

    @Autowired
    private IGeConSocialTopicsService geConSocialTopicsService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult add(@RequestBody GeConSocialTopics geConSocialTopics) {
        if(geConSocialTopicsService.verifyName(geConSocialTopics.getName())){
            return CommonResult.error("只允许同相话题开启一个");
        }
        geConSocialTopics.setStatus("1");
        geConSocialTopicsService.save(geConSocialTopics);
        return CommonResult.success("添加成功");
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult edit(@RequestBody GeConSocialTopics geConSocialTopics) {
        if(geConSocialTopicsService.verifyName(geConSocialTopics.getName())){
            return CommonResult.error("只允许同相话题开启一个");
        }
        geConSocialTopicsService.updateById(geConSocialTopics);
        return CommonResult.success("修改成功");
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult delete(String id) {
        geConSocialTopicsService.removeById(id);
        return CommonResult.success("删除成功");
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConSocialTopicsVO>> list(SocialTopicsQuery socialTopicsQuery) {
        PageInfo<GeConSocialTopicsVO> list = geConSocialTopicsService.pageList(socialTopicsQuery);
        return CommonResult.success(String.valueOf(list.getTotal()),list.getList());
    }

    @PostMapping(value = "/review")
    @ApiOperation("开关")
    public CommonResult review(@RequestBody GeConSocialTopics geConSocialTopics) {
        if(Objects.equals(geConSocialTopics.getStatus(),"1")){
            if(geConSocialTopicsService.verifyName(geConSocialTopics.getName())){
                return CommonResult.error("只允许同相话题开启一个");
            }
        }
        geConSocialTopicsService.updateById(geConSocialTopics);
        return CommonResult.success("修改成功");
    }

    @GetMapping(value = "/miniProgramList")
    @ApiOperation("小程序话题列表查询")
    public CommonResult<List<GeConSocialTopics>> miniProgramList() {
        List<GeConSocialTopics> list = new ArrayList<>();
        GeConSocialTopics geConSocialTopics = new GeConSocialTopics();
        geConSocialTopics.setId("1828277176473694209");
        geConSocialTopics.setName("热门");

        QueryWrapper<GeConSocialTopics> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","1");

        list.add(geConSocialTopics);
        list.addAll(geConSocialTopicsService.list(queryWrapper));
        CommonResult<List<GeConSocialTopics>> result = new CommonResult<>();
        result.setCode(200);
        result.setData(list);
        result.setMessage(String.valueOf(list.size()));
        return result;
    }

    @GetMapping(value = "/getTopicNumsAndReplyNums")
    @ApiOperation("获取各类别话题数量及下面的评论数")
    public CommonResult<SocialTopicNumsVO> getTopicNumsAndReplyNums(@RequestParam(name = "date",required = false) String date,
                                                                    @RequestParam(name = "pageNum",required = true,defaultValue = "1")Integer pageNum,
                                                                    @RequestParam(name = "pageSize",required = true,defaultValue = "10")Integer pageSize) {
        log.info("综管-获取各类别话题数量及下面的评论数:{}",date);
        return geConSocialTopicsService.getTopicNumsAndReplyNums(date,pageNum,pageSize);
    }

}
